/***************************************************************************
 创建者: 华磊
 开始时间: 2020.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef ZEROMANAGEMENT_H
#define ZEROMANAGEMENT_H
#include "motionmessage.h"
#include "JointDecouple/jointdecouple.h"
#include <qdebug.h>
#include "domparser.h"
#include "md5check.h"


enum E_ZERO_TASK_MODE
{
    E_ZERO_TASK_MODE_ETHERCAT,
    E_ZERO_TASK_MODE_INNER,
};
/**
 * @brief The ZeroManagement class 机器人所有轴的零点管理
 */
class ZeroManagement:DomParser
{
public:
    ZeroManagement(QString fileNameIn, int robotTypeIn, int robotIdIn, int jointNumIn
                   , MotionMessage *motionMessageIn, JointDecouple *jointDecoupleIn);

public:
    E_ZERO_TASK_MODE getZeroTaskMode();
    /**
     * @brief calibrateJoint_noSave 设定关节为正确的值
     *
     * @param currentJointValue //mm degree
     * @return
     */
    int calibrateJointByAiEncoder(int axisId, double aiValue);
    bool isAbsoulteMotor(int axisId);
    int getZeroMovingType(EM_ZERO_MOVINT_TYPE &zeroMovingTypeOut);
    int startGoZero(int axisId);
    int startGoZero();
    int haltGoZero();
    int getGoZeroAxis(int &axisId);
    int changeToNextGoZeroAxis();

    int loopRealTime();
    int loopCommonTime();
    int getCalibrateInformation(struct S_AXISGROUP_ZERO_INFORMATION& zeroInformationOut);
    int validateZeroData();
    int axisLosePower();
    int axisLosePower(int axisId);
    int calibrateRobotByOffset(std::vector<double> offsetIn);
    int calibrateRobot();
    int calibrateRobot(int axisId);
    bool isAxisZeroOk();
    bool isOnlyAxisZeroOk();
    bool isAxisZeroOk(int axisId);
    int getAxisZeroState(int axisId);
    QVector<int> getAbsoluteZeroMotorValue();
    QVector<double> getJointMechanicalZeroValue();
    int getZeroReturnParameter(QVector<ZeroReturnParameter> &parameterOut);


private:
    int checkFile();
    int createMd5File();
    int readFile();
    int writeFile();
    int checkIncrementEncoder();

    void addMsg(int messageLevel, std::string componentName, std::string messageType, int messageCode
                , int _robotId, int parameter1=0,int parameter2=0);

private:
    int jointNum;//轴的个数
    int robotType;//机器人类型
    int robotId;
    QVector<int> isAbsoluteEncoder;
    QVector<ZeroReturnParameter> zeroReturnParameterList;
    QVector<int> absoluteZeroMotorValue;//机器人ＤＨ算法零位电机值　pulse
    QVector<double> jointMechanicalZeroValue;//机器人机械零点关节值，相对于ＤＨ算法有偏移　mm degree,用于机器人校准零位（机械零位）与ＤＨ零位不重合的场景。
    QVector<int> axisZeroState;
    JointDecouple *jointDecouple;
    QString fileName;
    ENUM_ZERO_MANAGEMENT_STATE status;
    MotionMessage *motionMessage;

    volatile EM_ZERO_MOVINT_TYPE zeroMovingType;
    volatile bool isHalt;
    volatile int currentZeroMovingAxisId;
    QVector<int> axisIdtoGoZero;
    E_ZERO_TASK_MODE zeroTaskMode;


};

#endif // ZEROMANAGEMENT_H
